import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
import Layout from "@/layout/Index.vue"

// 定义路由，此处为Array数组，数据类型为RouteRecordRaw
const routes: Array<RouteRecordRaw> = [];

// 固定路由,不需要权限判断
export const constantRoutes: Array<RouteRecordRaw> = [
    {
        path: "/",
        redirect: "/index", // 重定向到路由path值为 /index 的路由
        name: "面板",
        component: Layout,
        meta: {
            title: "首页看板",
            icon: 'HomeFilled',
            affix: true,
            noKeepAlive: true,
        },
        children: [
            {
                path: "/index",
                name: "首页",
                component: () => import("@/views/dashboard/Index.vue"),
                meta: {
                    title: "首页",
                    hidden: true,
                },
            },
        ],
    },
    {
        path: "/echarts",
        name: "Echarts",
        component: Layout,
        meta: {
            title: "Echarts",
            icon: 'HomeFilled',
            affix: true,
            noKeepAlive: true,
        },
        children: [
            {
                path: "/barEcharts",
                name: "barEcharts",
                component: () => import("@/views/echarts/BarEcharts.vue"),
                meta: {
                    title: "柱状图",
                },
            },
            {
                path: "/lineEcharts",
                name: "LineEcharts",
                component: () => import("@/views/echarts/LineEcharts.vue"),
                meta: {
                    title: "折线图",
                },
            },
            {
                path: "/pineEcharts",
                name: "PineEcharts",
                component: () => import("@/views/echarts/PineEcharts.vue"),
                meta: {
                    title: "饼状图",
                },
            },
        ],
    },
    {
        path: "/errorPage",
        name: "ErrorPage",
        component: Layout,
        meta: {
            title: "异常页面",
            icon: "Menu",
        },
        children: [
            {
                path: "/404Page",
                name: "404Page",
                component: () => import("@/views/error/404.vue"),
                meta: {
                    title: "404额面",
                    icon: "Position",
                },
            },
            {
                path: "/401Page",
                name: "401Page",
                component: () => import("@/views/error/401.vue"),
                meta: {
                    title: "401页面",
                    icon: "Position",
                },
            },
        ],
    },
    {
        path: '/menu2',
        name: 'Menu2Demo',
        component: Layout,
        meta: {
            title: "Example",
            icon: "Position",
        },
        children: [
            {
                path: "/menu2/test",
                name: "Test",
                component: () => import("@/views/user/user.vue"),
                meta: {
                    title: "测试页",
                    icon: "Position",
                    affix: true,
                    noKeepAlive: true,
                },
            },
            {
                path: "/register",
                name: "注册",
                component: () => import("@/views/register/RegisterForm.vue"),
                meta: {
                    title: "用户注册",
                    hidden: false
                }
            }
        ],
    },
    // 当hidden为true时不会在菜单栏显示
    {
        path: "/login",
        name: "Login",
        component: () => import("@/views/login/UserLogin.vue"),
        meta: {
            title: "登录",
            hidden: true,
        },
    },
    {
        path: "/codelogin",
        name: "Login",
        component: () => import("@/views/login/phone/CodeLogin.vue"),
        meta: {
            title: "登录",
            hidden: true,
        },
    },
    {
        path: "/401",
        name: "401",
        component: () => import("@/views/error/401.vue"),
        meta: {
            title: "menu2额面",
            hidden: true,
        },
    },
    {
        path: "/404",
        name: "404",
        component: () => import("@/views/error/404.vue"),
        meta: {
            title: "menu2额面",
            hidden: true,
        },
    },
    {
        path: "/userinfo",
        name: "个人中心",
        component: () => import("@/views/user/UserInfo.vue"),
        meta: {
            title: "menu2额面",
            hidden: true,
        },
    },
];

// 动态路由，需要根据权限确定路由
export const asyncRoutes: Array<RouteRecordRaw> = [];

// 创建路由
const router = createRouter({
    history: createWebHistory(),
    routes: constantRoutes, // 将定义的路由传入
    strict: false,
});

//
const resetRoutes = () => {
    router.getRoutes().forEach((routes) => {
        const {name} = routes;
        if (name) {
            router.hasRoute(name) && router.removeRoute(name);
        }
    });
};
// 将创建的router路由暴露，使其在其他地方可以被引用
export default router;
